<?php
include_once './config/config.php';

$loginAdmin = $_COOKIE['loginAdmin'] ? $_COOKIE['loginAdmin'] : '';

if (empty($loginAdmin)) {
    ShowMsg("请先登录", "login.php");
    exit;
}

/** 
 * todo 通过cookie里的加密信息与数据库拿出来的数据加密对比
 * todo 来判断是否有登录
 */
$sql = "SELECT * FROM `pre_admin`";
$adminList = queryAll($sql);
$adminId = '';
foreach ($adminList as $item) {
    $reAdmin = md5($item['id'].$item['salt']);
    
    if ($loginAdmin == $reAdmin) {
        $adminId = $item['id'];
        break;
    }
}

// todo 重新从数据库查询登录用户的信息
$findSql = "SELECT * FROM `pre_admin` WHERE `id` = '$adminId'";
$adminData = query($findSql);

if (!$adminData) {
    setcookie("loginAdmin",null);
    ShowMsg("非法登录","login.php");
    exit;
}

?>

<div class="navbar">
    <div class="navbar-inner">
        <ul class="nav pull-right">
            <li>
                <a href="javascript:;" role="button" class="username">
                    <i class="icon-user"></i>admin
                </a>
            </li>
            <li>
                <a href="javascript:;" class="hidden-phone visible-tablet visible-desktop logout" role="button" style="color:#fff;">Logout</a>
            </li>
        </ul>
        <a class="brand" href="index.html"><span class="second">Admin</span></a>
    </div>
</div>

<script src="assets/lib/jquery-1.8.1.min.js"></script>
<script>
    // let data = JSON.parse(sessionStorage.getItem('data'));
    // if (!data) {
    //     alert("请先登录");
    //     location.href = "login.php";
    // }
    // let username = "<i class='icon-user'></i>"+data.username;
    // $(".username").html(username);

    $(".logout").on("click", function(e) {
        e.preventDefault();
        console.log($(this));

        let res = confirm("确认退出后台管理系统？");
        if (res) {
            sessionStorage.removeItem("loginAdmin");
            alert("退出成功");
            location.href = "login.php";
        }else{
            alert("退出失败");
            location.reload();
        }
    })
</script>